package com.buba.springcloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.springcloud.mapper.MaterialMapper;
import com.buba.springcloud.pojo.CommonResult;
import com.buba.springcloud.pojo.Payment;
import com.buba.springcloud.pojo.SuppliesAround;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@RestController
@Slf4j
public class SuppliesAroundController {

    //注入服务发现的注解
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private MaterialMapper materialMapper;

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "SuppliesAround/consul")
    public String paymentConsul() {
        return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString();
    }

    @CrossOrigin(methods = RequestMethod.GET)
    @GetMapping("/SuppliesAround/get/{id}")
    public CommonResult queryById(@PathVariable("id") Long id){
        SuppliesAround suppliesAround = materialMapper.selectById(id);
        log.info("***************查询成功*********"+suppliesAround);
        if(suppliesAround!=null){
            return new CommonResult(200,"查询成功",suppliesAround);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    @CrossOrigin()
    @GetMapping("/SuppliesAround/getList")
    public CommonResult getList(){
        QueryWrapper<SuppliesAround> queryWrapper = new QueryWrapper<>();
        List<SuppliesAround> list = materialMapper.selectList(queryWrapper);
        log.info("***************查询成功*********"+list);
        if(list!=null){
            return new CommonResult(200,"查询成功",list);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    //获取服务信息
    @GetMapping("/SuppliesAround/discovery")
    public  Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String s : services){
            log.info("********注册到eureka中的服务中有:"+services);
        }
        List <ServiceInstance> instances = discoveryClient.getInstances("MCROSERVICE-PAYMENT");
        for (ServiceInstance s: instances) {
            log.info("当前服务的实例有"+s.getServiceId()+"\t"+s.getHost()+"\t"+s.getPort()+"\t"+s.getUri());
        }
        return this.discoveryClient;
    }

}
